package com.liehu.business.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liehu.business.pojo.Article;
import com.liehu.business.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/article")
@CrossOrigin //跨域处理
@Api(value = "/article",description = "文章")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    private HttpServletRequest request;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 查询所有
     */
    @ApiOperation(value = "查询全部文章", notes = "查询全部文章")//描述
    @GetMapping()
    public Result findAll(){
        return  new Result(true, StatusCode.OK,"查询成功！",articleService.findAll());
    }
    /**
     * 根据id查询
     */
    @ApiOperation(value = "根据id查询", notes = "根据id查询")//描述
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id){
        Claims claims_user = (Claims) request.getAttribute("claims_user");
        String userId = claims_user.getId();//用户id
        String  thumbup= (String)redisTemplate.opsForValue().get("thumbup" + userId);
        if(thumbup==null){
            return  new Result(true, StatusCode.OK,"查询成功！",articleService.findById(id));
        }else{
            return  new Result(true, StatusCode.DOK,"查询成功且用户已点赞文章！",articleService.findById(id));
        }

    }
    /**
     * 分页查询
     */
    @ApiOperation(value = "分页查询", notes = "不带条件的分页查询")//描述
    @GetMapping("/{page}/{size}")
    public Result findByPage(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        Page<Article> list = articleService.findByPage(page, size);
        return  new Result(true, StatusCode.OK,"查询成功！",new PageResult<Article>(list.getTotal(),list.getRecords()));
    }
    /**
     * 带条件的分页查询
     */
    @ApiOperation(value = "分页查询", notes = "带条件的分页查询")//描述
    @PostMapping("/search/{page}/{size}")
    public Result findByPageSearch(@RequestBody Article article,@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        Page<Article> list = articleService.findByPage(article, page, size);
        return  new Result(true, StatusCode.OK,"查询成功！",new PageResult<Article>(list.getTotal(),list.getRecords()));
    }
    /**
     * 插入一条数据/保存
     */
    @ApiOperation(value = "插入一条数据/保存", notes = "插入一条数据/保存")//描述
    @PostMapping("save")
    public Result save(@RequestBody Article article){
        articleService.save(article);
        return  new Result(true, StatusCode.OK,"新增成功！");
    }

    /**
     * 插入一条数据/发布
     */
    @ApiOperation(value = "插入一条数据/发布", notes = "插入一条数据/发布")//描述
    @PostMapping("publish")
    public Result publish(@RequestBody Article article){
        articleService.publish(article);
        return  new Result(true, StatusCode.OK,"新增成功！");
    }

    /**
     * 根据id修改记录
     * @param article
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id修改记录/", notes = "根据id修改记录")//描述
    @PutMapping("/{id}")
    public Result update(@RequestBody Article article,@PathVariable("id") String id){
        article.setId(id);//给reply植入id
        articleService.update(article);
        return  new Result(true, StatusCode.OK,"修改成功！");
    }
    /**
     * 根据id删除
     */
    @ApiOperation(value = "根据id删除", notes = "根据id删除")//描述
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id){
        articleService.deleteById(id);
        return  new Result(true, StatusCode.OK,"删除成功！");
    }

    /**
     * 点赞文章
     */
    @ApiOperation(value = "根据文章id点赞", notes = "根据文章id点赞")//描述
    @DeleteMapping("/{id}")
    public Result Thumbup(@PathVariable("id") String id){
        if(articleService.thumbupById(id)){
            return  new Result(true, StatusCode.DOK,"点赞成功！");
        }else{
            return  new Result(true, StatusCode.QOK,"取消点赞成功！");
        }

    }
}
